package com.dongdong.service.impl;

import cn.hutool.core.util.StrUtil;
import com.dongdong.mapper.TbBrandMapper;
import com.dongdong.model.TbBrand;
import com.dongdong.model.TbBrandExample;
import com.dongdong.service.BrandService;
import com.dongdong.vo.BrandVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jnlp.BasicService;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private TbBrandMapper brandMapper;

    @Override
    public List<BrandVO> getBrandList() {
        List<TbBrand> brands = brandMapper.selectByExample(null);
        List<BrandVO> voList = brands.stream().map((x) -> {
            BrandVO vo = new BrandVO();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());
        return voList;
    }

    @Override
    public List<BrandVO> getBrandListLike(BrandVO brandVO) {
        TbBrandExample example = new TbBrandExample();
        TbBrandExample.Criteria criteria = example.createCriteria();
        if (!StrUtil.isEmpty(brandVO.getName())){
            criteria.andNameLike("%"+brandVO.getName()+"%");
        }
        if (!StrUtil.isEmpty(brandVO.getFirstChar())){
            criteria.andNameLike("%"+brandVO.getFirstChar()+"%");
        }
        List<TbBrand> tbBrands = brandMapper.selectByExample(example);
        List<BrandVO> voList = tbBrands.stream().map((x) -> {
            BrandVO vo = new BrandVO();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());
        return voList;
    }

    @Override
    public void addBrand(BrandVO brandVO) {
        TbBrand brand = new TbBrand();
        BeanUtils.copyProperties(brandVO,brand);
        brandMapper.insertSelective(brand);
    }

    @Override
    public BrandVO getBrandById(Long bid) {
        TbBrand tbBrand = brandMapper.selectByPrimaryKey(bid);
        BrandVO vo = new BrandVO();
        BeanUtils.copyProperties(tbBrand,vo);
        return vo;
    }

    @Override
    public void updateBrand(BrandVO brandVO) {
        TbBrand brand = new TbBrand();
        BeanUtils.copyProperties(brandVO,brand);
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public void deleteById(Long bid) {
        brandMapper.deleteByPrimaryKey(bid);
    }

    @Override
    public void deleteBatch(List<Long> bids) {
        brandMapper.deleteBatch(bids);
    }


}
